/* System Config.c */

/* Includes ------------------------------------------------------------------*/
//#include "main.h"
#include "system_stm32f10x.h"
#include "stm32f10x.h"


/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/

//fenris 20110128
/**
 * @brief	Interrupt vector table setting, clear all setting
 * @param	none
 * @return	none
 * @note
 */
static void NVIC_Configuration(void)
{
	/* allocate interrupt vector table */
	NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x2000);

	/* reset EXTI to default */
	EXTI_DeInit();
    
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
}

/**
 * @brief	system clock setting
 * @param	none
 * @return	none
 * @note
 */
void RCC_Configuration(void)
{
	/* RCC system reset(for debug purpose) */
	RCC_DeInit();

	/* Enable HSE */
	RCC_HSEConfig(RCC_HSE_ON);

	/* Wait till HSE is ready */
	if(RCC_WaitForHSEStartUp() == SUCCESS)
	{
		/* Enable Prefetch Buffer */
		//FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

		/* Enable Prefetch Buffer */
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

        /* Flash 2 wait state */
        FLASH_SetLatency(FLASH_Latency_2);
 
        /* HCLK = SYSCLK */
        //RCC_HCLKConfig(RCC_SYSCLK_Div1); 
        RCC_HCLKConfig(RCC_SYSCLK_Div2); 
  
        /* PCLK2 = HCLK */
        RCC_PCLK2Config(RCC_HCLK_Div1); 
        //RCC_PCLK2Config(RCC_HCLK_Div4); 

        /* PCLK1 = HCLK/2 */
        RCC_PCLK1Config(RCC_HCLK_Div2);
        //RCC_PCLK1Config(RCC_HCLK_Div8);
        
        /* PLLCLK = 8MHz * 9 = 72 MHz */
        RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

		/* Enable PLL */
		RCC_PLLCmd(ENABLE);

		/* Wait till PLL is ready */
		while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {}

		/* Select PLL as system clock source */
		RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

		/* Wait till PLL is used as system clock source */
		while(RCC_GetSYSCLKSource() != 0x08) {}
	}

	RCC_APB1PeriphClockCmd(0x00
							| RCC_APB1Periph_PWR
							| RCC_APB1Periph_BKP
							, ENABLE);
}

/**
 * @brief	GPIO setting
 * @param	none
 * @return	none
 * @note
 */
static void GPIO_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	/* Enable GPIOD and GPIOE clock */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB
							| RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOD, ENABLE );


	/* Disable the Serial Wire Jtag Debug Port SWJ-DP */
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);

	/* PA  */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	/* PB  */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	/* PC  */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
	GPIO_Init(GPIOC, &GPIO_InitStructure);
    /* PE  */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
	GPIO_Init(GPIOE, &GPIO_InitStructure);
    /* PD  */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
	GPIO_Init(GPIOD, &GPIO_InitStructure);
}

/**
 * @brief	RTC setting
 * @param	none
 * @return	none
 * @note
 */
static void RTC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	EXTI_InitTypeDef EXTI_InitStructure;

	/* Enable the RTC Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel				= RTC_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority	= 10;   //IRQ_RTC;
	NVIC_InitStructure.NVIC_IRQChannelCmd			= ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	/* Enable the RTC Alarm Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel				= RTCAlarm_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority	= 11;   //IRQ_RTC_ALARM;
	NVIC_InitStructure.NVIC_IRQChannelCmd			= ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	/* config RTC Alarm as EXTI17 for wakeup STOP Mode */
	EXTI_InitStructure.EXTI_Line    = EXTI_Line17;
	EXTI_InitStructure.EXTI_Mode	= EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger	= EXTI_Trigger_Rising;
	EXTI_InitStructure.EXTI_LineCmd	= ENABLE;
	EXTI_Init(&EXTI_InitStructure);

	/* Allow access to BKP Domain */
	PWR_BackupAccessCmd(ENABLE);

	/* Enable LSE */
	RCC_LSEConfig(RCC_LSE_ON);

	/* Wait till LSE is ready */
	while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {}

	/* Select LSE as RTC Clock Source */
	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

	/* Enable RTC Clock */
	RCC_RTCCLKCmd(ENABLE);

	/* Wait for RTC registers synchronization */
	RTC_WaitForSynchro();

	/* Set RTC prescaler: set RTC period to 1sec */
	RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */

	/* Wait until last write operation on RTC registers has finished */
	RTC_WaitForLastTask();

	/* Enable the RTC Second */
	RTC_ITConfig(RTC_IT_SEC | RTC_IT_ALR, ENABLE);

	/* Wait until last write operation on RTC registers has finished */
	RTC_WaitForLastTask();
	
}

/**
 * @brief	system tick setting
 * @param	none
 * @return	none
 * @note
 */
static void SysTick_Configuration(void)
{

	/* 1ms = SYS_CLK / 8000 */
	if(SysTick_Config((SystemCoreClock / 8000)))
    {
        while(1);
    }
	
	/* Select HCLK/8 as SysTick clock source */
	SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
}

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void System_Init(void)
{  
    NVIC_Configuration();
	RCC_Configuration();
	GPIO_Configuration();
	RTC_Configuration();
	SysTick_Configuration();
}

/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
